package com.jyxd.web.controller.czform;

import com.alibaba.fastjson.JSON;
import com.jyxd.web.data.czform.ECMOOutput;
import com.jyxd.web.data.dto.HttpResult;
import com.jyxd.web.service.czform.ECMOOutputService;
import com.jyxd.web.util.HttpCode;
import com.jyxd.web.util.UUIDUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import net.sf.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Api(tags = "ecmo记录单出量")
@Controller
@RequestMapping(value = "/ecmoOutput")
public class ECMOOutputController {

    private static Logger logger = LoggerFactory.getLogger(ECMOOutputController.class);

    @Autowired
    private ECMOOutputService ecmoOutputService;

    /**
     * 增加一条ecmo记录单出量信息
     *
     * @return
     */
    @ApiOperation("增加一条ecmo记录单出量信息")
    @RequestMapping(value = "/insert", method = RequestMethod.POST)
    @ResponseBody
    public HttpResult insert (@RequestBody ECMOOutput ecmoOutput) {
        JSONObject json = new JSONObject();
        json.put("code", HttpCode.FAILURE_CODE.getCode());
        json.put("data", new ArrayList<>());
        ecmoOutput.setId(UUIDUtil.getUUID());
        boolean flag = ecmoOutputService.insert(ecmoOutput);
        if (flag) {
            return new HttpResult(200, "添加成功");
        } else {
            return new HttpResult(200, "添加失败");
        }
    }


    /**
     * 编辑ecmo记录单信息
     *
     * @param map
     * @return
     */
    @ApiOperation("编辑ecmo记录单出量信息")
    @RequestMapping(value = "/edit", method = RequestMethod.POST)
    @ResponseBody
    public String edit (@RequestBody(required = false) Map<String, Object> map) {
        JSONObject json = new JSONObject();
        json.put("code", HttpCode.FAILURE_CODE.getCode());
        if (map != null && map.containsKey("id") && map.containsKey("status")) {
            ECMOOutput ecmoOutput = ecmoOutputService.queryData(map.get("id").toString());
            if (ecmoOutput != null) {
                ecmoOutput.setStatus((int) map.get("status"));
                ecmoOutputService.update(ecmoOutput);
            } else {
                return json.toString();
            }
        }
        json.put("code", HttpCode.OK_CODE.getCode());
        return json.toString();
    }

    /**
     * 删除ecmo记录单
     *
     * @param map
     * @return
     */
    @ApiOperation("删除一条ecmo记录单出量信息")
    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    @ResponseBody
    public String delete (@RequestBody(required = false) Map<String, Object> map) {
        JSONObject json = new JSONObject();
        json.put("code", HttpCode.FAILURE_CODE.getCode());
        if (map != null && map.containsKey("id")) {
            ECMOOutput ecmoOutput = ecmoOutputService.queryData(map.get("id").toString());
            if (ecmoOutput != null) {
                ecmoOutput.setStatus(- 1);
                ecmoOutputService.update(ecmoOutput);
            } else {
                return json.toString();
            }
        }
        json.put("code", HttpCode.OK_CODE.getCode());
        return json.toString();
    }

    /**
     * 根据主键id查询ecmo记录单信息
     *
     * @param map
     * @return
     */
    @ApiOperation("根据主键id查询ecmo记录单出量信息")
    @RequestMapping(value = "/queryData", method = RequestMethod.POST)
    @ResponseBody
    public String queryData (@RequestBody(required = false) Map<String, Object> map) {
        JSONObject json = new JSONObject();
        json.put("code", HttpCode.FAILURE_CODE.getCode());
        json.put("data", new ArrayList<>());
        if (map != null && map.containsKey("id")) {
            ECMOOutput ecmoOutput = ecmoOutputService.queryData(map.get("id").toString());
            if (ecmoOutput != null) {
                json.put("data", JSON.toJSONString(ecmoOutput));
            }
        }
        json.put("code", HttpCode.OK_CODE.getCode());
        return json.toString();
    }

    /**
     * 根据条件分页查询ecmo记录单信息列表（也可以不分页）
     *
     * @param map
     * @return
     */
    @ApiOperation("根据条件分页查询ecmo记录单出量信息列表（也可以不分页）")
    @RequestMapping(value = "/queryList", method = RequestMethod.POST)
    @ResponseBody
    public String queryList (@RequestBody(required = false) Map<String, Object> map) {
        JSONObject json = new JSONObject();
        json.put("code", HttpCode.FAILURE_CODE.getCode());
        json.put("data", new ArrayList<>());
        if (map != null && map.containsKey("start")) {
            int totalCount = ecmoOutputService.queryNum(map);
            map.put("start", ((int) map.get("start") - 1) * (int) map.get("size"));
            json.put("totalCount", totalCount);
        }
        List<ECMOOutput> list = ecmoOutputService.queryList(map);
        if (list != null && list.size() > 0) {
            json.put("data", JSON.toJSONString(list));
        }
        json.put("code", HttpCode.OK_CODE.getCode());
        return json.toString();
    }


}
